package com.fyyg.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fyyg.common.code.Menu;
import com.fyyg.common.util.Config;
import com.fyyg.dal.TSysResourceMapper;
import com.fyyg.dal.TSysRoleMapper;
import com.fyyg.dal.TSysUserRoleMapper;
import com.fyyg.interfaces.TSysResourceService;
import com.fyyg.model.TSysResource;
import com.fyyg.model.TSysUser;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service("sysResourceService")
public class TSysResourceServiceImpl implements TSysResourceService{
	
	@Resource
	private TSysResourceMapper resourceMapper;
	@Resource
	private TSysUserRoleMapper userRoleMapper;
	@Resource
	private TSysRoleMapper roleMapper;

	@Override
	public List<Menu> findTree(TSysUser currentUser) {
		List<Menu> trees = Lists.newArrayList();
        // 超级管理
        if (currentUser.getLoginname().equals("admin")) {
            List<TSysResource> resourceFather = resourceMapper.findResourceAllByTypeAndPidNull(0);//0菜单
            if (resourceFather == null) {
                return null;
            }

            for (TSysResource resourceOne : resourceFather) {
                Menu treeOne = new Menu();

                treeOne.setId(resourceOne.getId());
                treeOne.setText(resourceOne.getName());
                treeOne.setIconCls(resourceOne.getIcon());
                treeOne.setAttributes(resourceOne.getUrl());
                List<TSysResource> resourceSon = resourceMapper.findResourceAllByTypeAndPid(0, resourceOne.getId());

                if (resourceSon != null) {
                    List<Menu> tree = Lists.newArrayList();
                    for (TSysResource resourceTwo : resourceSon) {
                        Menu treeTwo = new Menu();
                        treeTwo.setId(resourceTwo.getId());
                        treeTwo.setText(resourceTwo.getName());
                        treeTwo.setIconCls(resourceTwo.getIcon());
                        treeTwo.setAttributes(resourceTwo.getUrl());
                        tree.add(treeTwo);
                    }
                    treeOne.setChildren(tree);
                } else {
                    treeOne.setState("closed");
                }
                trees.add(treeOne);
            }
        }else {
        	// 普通用户
            Set<TSysResource> resourceIdList = Sets.newHashSet();
            List<Integer> roleIdList = userRoleMapper.findRoleIdListByUserId(currentUser.getId());//根据用户ID获取角色ID
            for (Integer i : roleIdList) {
                List<TSysResource> resourceIdLists = roleMapper.findResourceIdListByRoleIdAndType(i);
                for (TSysResource resource: resourceIdLists) {
                    resourceIdList.add(resource);
                }
            }
            resourceIdList.addAll(resourceIdList);
            for (TSysResource resource : resourceIdList) {
                if (resource != null && resource.getPid() == null) {
                    Menu treeOne = new Menu();
                    treeOne.setId(resource.getId());
                    treeOne.setText(resource.getName());
                    treeOne.setIconCls(resource.getIcon());
                    treeOne.setAttributes(resource.getUrl());
                    List<Menu> tree = Lists.newArrayList();
                    for (TSysResource resourceTwo : resourceIdList) {
                        if (resourceTwo.getPid() != null && resource.getId().longValue() == resourceTwo.getPid().longValue()) {
                        	Menu treeTwo = new Menu();
                            treeTwo.setId(resourceTwo.getId());
                            treeTwo.setText(resourceTwo.getName());
                            treeTwo.setIconCls(resourceTwo.getIcon());
                            treeTwo.setAttributes(resourceTwo.getUrl());
                            tree.add(treeTwo);
                        }
                    }
                    treeOne.setChildren(tree);
                    trees.add(treeOne);
                }
            }
		}
        return trees;
	}

	@Override
	public List<TSysResource> findResourceAll() {
		return resourceMapper.findResourceAll();
	}

	@Override
	public List<Menu> findAllTree() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String findAllTrees(Integer roleId) {
		JSONArray jsonArray = new JSONArray();
		
        // 查询所有的一级树
        List<TSysResource> resources = resourceMapper.findResourceAllByTypeAndPidNull(Config.RESOURCE_MENU);
        if (resources == null) {
            return null;
        }
        for (TSysResource resourceOne : resources) {
        	JSONObject one = new JSONObject();
        	one.put("id", resourceOne.getId());
        	one.put("pId", 0);
        	one.put("name", resourceOne.getName());
        	one.put("open", true);
        	jsonArray.add(one);
            List<TSysResource> resourceSon = resourceMapper.findResourceAllByTypeAndPid(Config.RESOURCE_MENU, resourceOne.getId());

            if (resourceSon == null) {
            } else {
                for (TSysResource resourceTwo : resourceSon) {
                	JSONObject two = new JSONObject();
                	two.put("id",resourceTwo.getId());
                	two.put("pId",resourceOne.getId());
                	two.put("name",resourceTwo.getName());
                	two.put("open", true);
                	jsonArray.add(two);

                    /***************************************************/
                    List<TSysResource> resourceSons = resourceMapper.findResourceAllByTypeAndPid(Config.RESOURCE_BUTTON, resourceTwo.getId());

                    if (resourceSons == null) {
                        //treeTwo.setState("closed");
                    } else {
                        List<Menu> treeThreeList = Lists.newArrayList();
                        for (TSysResource resourceThree : resourceSons) {
                        	JSONObject three = new JSONObject();
                        	three.put("id",resourceThree.getId());
                        	three.put("pId",resourceTwo.getId());
                        	three.put("name",resourceThree.getName());
                        	jsonArray.add(three);
                        }
                    }
                    /***************************************************/
                }
            }
        }
        
        List<Map<String, Object>> list = roleMapper.findResourceListByRoleId(roleId);
        if(list!=null && list.size()>0){
        	for (int i = 0; i < jsonArray.size(); i++) {
        		JSONObject json = jsonArray.getJSONObject(i);
        		Integer resourceId = json.getInteger("id");
        		for (Map<String, Object> map : list) {
            		Integer id = Integer.parseInt(map.get("id").toString());
            		if(resourceId.equals(id)){
            			json.put("checked", true);
            		}
            	}
    		}
        }
        return jsonArray.toString();
	}


}
